import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '医院名称',		name: 'hosp_id', type: 'select',nameStr:'name' },
			{ label: '医院性质',		name: 'nature',	type: 'select', loading:true,idStr:'code',nameStr:'name' },
			{ label: '医院代码',		name: 'hosp_code' },
			{ label: '医院级别',		name: 'level',	type: 'select', loading:true,idStr:'code',nameStr:'name' },
			{ label: '区域',			name: 'region',	type: 'select', loading:true,idStr:'value',nameStr:'name' },
		],
	}
	
	model = {}
	query = { type:'my' }
	
	componentDidMount(){
		const { type } = this.props
		if(type){ this.query.type = type }
		
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
		
		$fn.fresh.on(()=>{
			this.fetch()
			this.getHosptal()
		})
	}
	componentWillUnmount(){
		$fn.fresh.remove()
	}
	// 获取医院
	getHosptal = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital', param: {all:'all'}, way:'get' } ) )
			//
			this.setState({ forms })
		})
	}
	
	// 获取下拉数据
	getSelect = async () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital', param: {all:'all'}, way:'get' } ) )
			// 区域
			$fn.setRow(forms, 'region', await f.getCache({ api:'bs_area' } ) )
			//医院性质
			$fn.setRow(forms, 'nature', await f.getDicCache({ code:44100 } ) )
			//医院级别
			$fn.setRow(forms, 'level', await f.getDicCache({ code:39000 } ) )
			//
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bs-hospital/businessHosp',{isIndex:true})
	
	// 表单
	cols = [
		{ title: '医院代码', 	dataIndex: 'hosp_code', 		...$fn.table.hosp_code },
		{ title: '医院名称', 	dataIndex: 'hosp_name', 		width:150 },
		{ title: '医院性质', 	dataIndex: 'nature_name', 		width:80 },
		{ title: '医院级别', 	dataIndex: 'level_name', 		width:80 },
		{ title: '业务员名称', 	dataIndex: 'salesman_user', 	...$fn.table.name },
		{ title: '创建时间', 	dataIndex: 'create_at', 		...$fn.table.time },
		{ title: '状态', 		dataIndex: 'enabled', 			width:80,  render: text => +text === 1 ? '审核通过' : '未审核' },
		TableControls.controls.call(this, { width: 200 })
	]
	
	// 表格操作按钮
	table_controls = (text,{ uuid,hosp_name }) => {
		const { type } = this.query
		return [
			TableControls.pushTo.call(this, '编辑',  `edit?uuid=${uuid}&edit=1` ),
			TableControls.pushTo.call(this, '查看',  `edit?uuid=${uuid}` ),
			type === 'my' && TableControls.pushTo.call(this, '物价管理',  `price-control?uuid=${uuid}&hosp_name=${$fn.encode(hosp_name)}` ),
		]
	}
	
	render(){
		const { title } = this.props
		return (
			<Page title={ title ? title : '我的医院客户'} >
				{/* 搜索 */}
				{ SearchForm.call(this) }
				{/* 表格 */}
				{ Table.call(this,{isIndex:true, isInit: window.isInit}) }
			</Page>
		)
	}
}

export default Index